#include "Tan.h"
#include "gui/TanContainer.h"
#include <cmath>
#include <QPointF>
#include <QMatrix>

namespace qtangram {
Tan::Tan(QPointF reference, QColor background, QPolygonF original): selected(false), polyScreen(original), polyCurrent(original), refPoint(reference), polyOriginal(original), backgroundColor(background) {

}

void Tan::recalculateCurrentPolygon()
{
    polyCurrent.clear();
    for (auto it = polyOriginal.begin(); it!= polyOriginal.end(); ++it)
    {
        polyCurrent.push_front(( (*it) ) + currentPoint - refPoint);
    }
    QMatrix m;
    m.rotate(angle);
    polyCurrent.translate(-currentPoint);
    for (auto it = polyCurrent.begin(); it!= polyCurrent.end(); ++it)
    {
        (*it)=(*it)*m;
    }
    polyCurrent.translate(currentPoint);
    if(flip)
    {
        for (auto it = polyCurrent.begin(); it!= polyCurrent.end(); ++it)
        {
            (*it).setX(  (*it).x() + 2.0*(currentPoint.x()-(*it).x()) );
        }
    }

}
void Tan::setDefaultPosition() {
    polyCurrent = polyOriginal;
    currentPoint = refPoint;
    angle=0;
    flip=false;
    selected=false;
    recalculateCurrentPolygon();
}
}
